﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.CustomerManage;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;

namespace MINERP.BLL.CustomerManage
{
    public class ShortOutReturnBLL
    {
        /// <summary>
        /// 获取旧产品信息
        /// </summary>
        /// <param name="inOrderProductid"></param>
        /// <returns></returns>
        public OrderInfoInfo GetOrderInfomation(int inOrderProductid)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OrderInfoInfo outProduct = null;
                orderInfo_BK orderProduct = db.orderInfo_BK.FirstOrDefault(p => p.OrderProductId == inOrderProductid);
                if (orderProduct == null)//还在生产的表中
                {
                    orderInfo order = db.orderInfo.FirstOrDefault(p => p.OrderProductId == inOrderProductid);
                    if (order != null)
                    {
                        //加载相关信息
                        order.Tb_OrderBagReference.Load();
                        order.WorkOrderFlowList.Load();
                        order.photoInfo.Load();
                        order.Tb_ProductReference.Load();
                        order.Tb_OrderBag.ShopReference.Load();
                        order.Tb_OrderBag.CustomAddress.Load();
                        ///
                        outProduct = new OrderInfoInfo() { CompleteTime = order.CompleteTime, CtmName = order.CtmName, Height = order.Height, OrderBagId = order.Tb_OrderBag.OrderBagId, OrderPath = order.OrderPath, OrderProductCode = order.OrderProductCode, OrderProductId = order.OrderProductId, OrderProductName = order.OrderProductName, OrilOrderPath = order.OrilOrderPath, PageNum = order.PageNum, ParentProductId = order.ParentProductId, PrePriceCount = (decimal)order.PrePriceCount, ProductId = order.Tb_Product.ProuctId, ProductNum = order.productNum, ProductType = order.ProductType, Remark = order.Remark, Status = order.status, Width = order.Width };
                        //商户 信息
                        outProduct.ShopInfo = new Shop_Info() { ShopId = order.Tb_OrderBag.Shop.ShopId, ShopName = order.Tb_OrderBag.Shop.shopName};
                        //outProduct.CustomAddress = new CustomAddress_Info() { AddressID = order.Tb_OrderBag };
                        List<CustomAddress_Info> customAddressList = new List<CustomAddress_Info>();
                        foreach (CustomAddress custAdd in order.Tb_OrderBag.CustomAddress)
                        {
                            CustomAddress_Info cuAdd = new CustomAddress_Info() { AddressID = custAdd.AddressID };
                            //cuAdd.AddressID
                            customAddressList.Add(cuAdd);
                        }

                        outProduct.OrderBage = new OrderBagInfo() { BagCode = order.Tb_OrderBag.BagCode, BagName = order.Tb_OrderBag.BagName, FetchTime=order.Tb_OrderBag.FetchTime,  CompleteTime=order.Tb_OrderBag.CompleteTime, OrderBagId=order.Tb_OrderBag.OrderBagId, OrderTime=order.Tb_OrderBag.OrderTime, Priority=order.Tb_OrderBag.Priority, Remark=order.Tb_OrderBag.Remark, ShopID=order.Tb_OrderBag.Shop.ShopId, Status=order.Tb_OrderBag.Status, Type=order.Tb_OrderBag.Type };
                         
                        //加载照片信息
                        List<PhotoInfo_Info> Photos = new List<PhotoInfo_Info>();
                        foreach (photoInfo photo in order.photoInfo)
                        {
                            photo.paperFaceReference.Load();
                            photo.paperTypeReference.Load();

                            PhotoInfo_Info photoInfoInfo = new PhotoInfo_Info() { Height = photo.Height, OldPicPath = photo.OldPicPath, OrderProductId = order.OrderProductId, PaperFaceId = photo.paperFace.PaperFaceID, PaperTypeID = photo.paperType.PaperTypeID, PhotoBarCode = photo.PhotoBarCode, PhotoId = photo.photoId, PhotoName = photo.photoName, PicPath = photo.picPath, PixHeight = photo.pixHeight, PixWidth = photo.pixWidth, PrinterId = (int)photo.printerId, Remark = photo.Remark, Status = photo.status, TotalNum = (int)photo.TotalNum, Width = photo.Width,ImageEnhancement=photo.imageEnhancement??false };
                            Photos.Add(photoInfoInfo);

                        }
                        outProduct.Photos = Photos;
                        //加载流程信息
                        List<WorkOrderFlowListInfo> listWofl = new List<WorkOrderFlowListInfo>();
                        foreach (WorkOrderFlowList wofl in order.WorkOrderFlowList)
                        {
                            wofl.tb_TechReference.Load();
                            //WorkOrderFlowListInfo wofli = new WorkOrderFlowListInfo() { AssignTime = (DateTime)wofl.AssignTime, CompletedTime = (DateTime)wofl.CompletedTime, OrderProductId = order.OrderProductId, ReceiveTime = (DateTime)wofl.ReceiveTime, Status = wofl.Status, TechId = wofl.tb_Tech.TechId, UserId = wofl.UserId, WorkFlowListId = wofl.WorkFlowListId, WorkOrder = wofl.WorkOrder, TechName = wofl.tb_Tech.TechName};
                            //listWofl.Add(wofli);
                            switch (wofl.tb_Tech.TechType)
                            {
                                case (int)TechType.DigitalOutput:
                                case (int)TechType.TiaoSe:
                                case (int)TechType.OutProduct:
                                    WorkOrderFlowListInfo wofli = new WorkOrderFlowListInfo() { AssignTime = (DateTime)wofl.AssignTime, CompletedTime = (DateTime)wofl.CompletedTime, OrderProductId = order.OrderProductId, ReceiveTime = (DateTime)wofl.ReceiveTime, Status = wofl.Status, TechId = wofl.tb_Tech.TechId, UserId = wofl.UserId, WorkFlowListId = wofl.WorkFlowListId, WorkOrder = wofl.WorkOrder, TechName = wofl.tb_Tech.TechName };
                                    listWofl.Add(wofli);
                                    break;
                                default:
                                    break;
                            }

                                    
                        }
                        outProduct.workFlows = listWofl;

                    }
                    else
                    {
                        //无此产品信息
                        return null;
                    }
                    return outProduct;
                }
                else//已经在备份表中了
                {
                    orderInfo_BK order = db.orderInfo_BK.FirstOrDefault(p => p.OrderProductId == inOrderProductid);
                    if (order != null)
                    {
                        //加载相关信息
                        order.Tb_OrderBag_BKReference.Load();
                        order.WorkOrderFlowList_BK.Load();
                        order.photoInfo_BK.Load();
                        order.Tb_ProductReference.Load();
                        order.Tb_OrderBag_BK.ShopReference.Load();
                        order.Tb_OrderBag_BK.CustomAddress_bk.Load();
                        ///
                        outProduct = new OrderInfoInfo() { CompleteTime = order.CompleteTime, CtmName = order.CtmName, Height = order.Height, OrderBagId = order.Tb_OrderBag_BK.OrderBagId, OrderPath = order.OrderPath, OrderProductCode = order.OrderProductCode, OrderProductId = order.OrderProductId, OrderProductName = order.OrderProductName, OrilOrderPath = order.OrilOrderPath, PageNum = order.PageNum, ParentProductId = order.ParentProductId, PrePriceCount = order.PrePriceCount??(decimal)0.00, ProductId = order.Tb_Product.ProuctId, ProductNum = order.productNum, ProductType = order.ProductType, Remark = order.Remark, Status = order.status, Width = order.Width };

                        //商户 信息
                        outProduct.ShopInfo = new Shop_Info() { ShopId = order.Tb_OrderBag_BK.Shop.ShopId, ShopName = order.Tb_OrderBag_BK.Shop.shopName };
                        List<CustomAddress_Info> customAddressList = new List<CustomAddress_Info>();
                        foreach (CustomAddress_bk custAdd in order.Tb_OrderBag_BK.CustomAddress_bk)
                        {
                            CustomAddress_Info cuAdd = new CustomAddress_Info() { AddressID = custAdd.AddressID };
                            //cuAdd.AddressID
                            customAddressList.Add(cuAdd);
                        }

                        outProduct.OrderBage = new OrderBagInfo() { BagCode = order.Tb_OrderBag_BK.BagCode, BagName = order.Tb_OrderBag_BK.BagName, FetchTime = order.Tb_OrderBag_BK.FetchTime, CompleteTime = order.Tb_OrderBag_BK.CompleteTime, OrderBagId = order.Tb_OrderBag_BK.OrderBagId, OrderTime = order.Tb_OrderBag_BK.OrderTime, Priority = order.Tb_OrderBag_BK.Priority, Remark = order.Tb_OrderBag_BK.Remark, ShopID = order.Tb_OrderBag_BK.Shop.ShopId, Status = order.Tb_OrderBag_BK.Status, Type = order.Tb_OrderBag_BK.Type };
                        //加载照片信息
                        List<PhotoInfo_Info> Photos = new List<PhotoInfo_Info>();
                        foreach (photoInfo_BK photo in order.photoInfo_BK)
                        {
                            photo.paperFaceReference.Load();
                            photo.paperTypeReference.Load();

                            PhotoInfo_Info photoInfoInfo = new PhotoInfo_Info() { Height = photo.Height, OldPicPath = photo.OldPicPath, OrderProductId = order.OrderProductId, PaperFaceId = photo.paperFace.PaperFaceID, PaperTypeID = photo.paperType.PaperTypeID, PhotoBarCode = photo.PhotoBarCode, PhotoId = photo.photoId, PhotoName = photo.photoName, PicPath = photo.picPath, PixHeight = photo.pixHeight, PixWidth = photo.pixWidth, PrinterId = photo.printerId??0, Remark = photo.Remark, Status = photo.status, TotalNum = photo.TotalNum ?? 0, Width = photo.Width };
                            Photos.Add(photoInfoInfo);

                        }
                        outProduct.Photos = Photos;
                        //加载流程信息
                        List<WorkOrderFlowListInfo> listWofl = new List<WorkOrderFlowListInfo>();
                        foreach (WorkOrderFlowList_BK wofl in order.WorkOrderFlowList_BK)
                        {
                            wofl.tb_TechReference.Load();
                            switch (wofl.tb_Tech.TechType)
                            {
                                case (int)TechType.DigitalOutput:
                                case (int)TechType.TiaoSe:
                                case (int)TechType.OutProduct:
                                    WorkOrderFlowListInfo wofli = new WorkOrderFlowListInfo() { AssignTime = (DateTime)wofl.AssignTime, CompletedTime = (DateTime)wofl.CompletedTime, OrderProductId = order.OrderProductId, ReceiveTime = (DateTime)wofl.ReceiveTime, Status = wofl.Status, TechId = wofl.tb_Tech.TechId, UserId = wofl.UserId, WorkFlowListId = wofl.WorkFlowListId, WorkOrder = wofl.WorkOrder, TechName = wofl.tb_Tech.TechName };
                                    listWofl.Add(wofli);
                                    break;
                                default:
                                    break;

                                    
                            }
                    
                            

                        }
                        outProduct.workFlows = listWofl;
                        return outProduct;
                    }
                    else
                    {
                        //无此产品信息
                        return null;
                    }
                }


            }


        }

        public List<PaperFace_Info> GetAllPaperFace()
        {
            List<PaperFace_Info> listFace = new List<PaperFace_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperFace> faceList = db.paperFace.ToList();
                foreach (paperFace pf in faceList)
                {
                    PaperFace_Info pfi = new PaperFace_Info();
                    pfi.PaperFaceID = pf.PaperFaceID;
                    pfi.Name = pf.Name;
                    listFace.Add(pfi);
                }
            }
            return listFace;
        }
        public List<PaperType_Info> GetAllPaperType()
        {
            List<PaperType_Info> listType = new List<PaperType_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperType> faceList = db.paperType.ToList();
                foreach (paperType pt in faceList)
                {
                    PaperType_Info pfi = new PaperType_Info();
                    pfi.PaperTypeID = pt.PaperTypeID;
                    pfi.Name = pt.Name;
                    listType.Add(pfi);
                }
            }
            return listType;
        }
        public List<PaperType_Info> GetAllPaperType(int inShopId)
        {
            List<PaperType_Info> listType = new List<PaperType_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p=>p.ShopId==inShopId);
                shop.Shop_Paper_Price.Load();


                foreach (Shop_Paper_Price spp in shop.Shop_Paper_Price)
                {
                    spp.paperTypeReference.Load();
                    PaperType_Info pti = new PaperType_Info();
                    pti.PaperTypeID = spp.paperType.PaperTypeID;
                    pti.Name = spp.paperType.Name;
                    listType.Add(pti);
                }
            }
            return listType;
        }

        public string  CreateOutReturn(OrderInfoInfo inOrder,int inStartTech,int inUserId)
        {

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                Tb_OrderBag orderBagNew = new Tb_OrderBag();//订单包信息
                orderBagNew.BagName = inOrder.OrderBage.BagName;
                orderBagNew.BagCode = "1";
                orderBagNew.CompleteTime = DateTime.Now;
                //orderBagNew.CustomAddress = inOrder.OrderBage.c
                orderBagNew.EditUserID = 0;
                orderBagNew.FetchTime = DateTime.Now;
                //orderBagNew.orderInfo = orderNew;
                orderBagNew.Priority = 11;
                orderBagNew.Remark = inOrder.OrderBage.Remark;
                orderBagNew.Shop = db.Shop.FirstOrDefault(p => p.ShopId == inOrder.ShopInfo.ShopId);
                orderBagNew.Status = (int)OrderBagStatus.Doing;
                orderBagNew.Type = 0;
                orderBagNew.users = db.users.FirstOrDefault(p => p.userID == inUserId);
                orderBagNew.OrderTime = DateTime.Now;
                //orderBagNew.

                orderInfo orderNew = new orderInfo();
                orderNew.CompleteTime = DateTime.Now;
                orderNew.CtmName = inOrder.CtmName + "_外部返单";
                orderNew.Height = inOrder.Height;
                orderNew.IsEdit = false;
                orderNew.OrderProductCode = "2";
                orderNew.MultiPageNum = 0;
                orderNew.OrderPath = inOrder.OrderPath;
                orderNew.OrderProductName = inOrder.OrderProductName+"_外部返单";
                orderNew.OrderType = (int)OrderInfoType.OutReturnInfo;
                orderNew.OrilOrderPath = inOrder.OrilOrderPath;
                orderNew.PageNum = inOrder.PageNum;
                orderNew.ParentProductId = inOrder.ParentProductId;
                orderNew.Priority = 11;
                orderNew.productNum = inOrder.ProductNum;
                orderNew.ProductType = inOrder.ProductType;
                orderNew.Remark = inOrder.Remark;
                orderNew.SinglePageNum = 0;
                orderNew.status = (int)OrderInfoStatus.Doing;
                orderNew.Tb_Product = db.Tb_Product.FirstOrDefault(p=>p.ProuctId==inOrder.ProductId);
                orderNew.Width = inOrder.Width;
                orderBagNew.orderInfo.Add(orderNew);
                List<photoInfo> listPhotos = new List<photoInfo>();//照片信息
                foreach (PhotoInfo_Info photo in inOrder.Photos.Where(p=>p.IsRebackPhoto).ToList())
                {
                    photoInfo photoNew = new photoInfo();
                    photoNew.Height = photo.Height;
                    photoNew.imageEnhancement = photo.ImageEnhancement;
                    photoNew.IsModifySize = false;
                    photoNew.PhotoBarCode = "3";
                    photoNew.OldPicPath = photo.OldPicPath;
                    photoNew.orderInfo = orderNew;
                    photoNew.paperFace = db.paperFace.FirstOrDefault(p=>p.PaperFaceID==photo.PaperFaceId);
                    photoNew.paperType = db.paperType.FirstOrDefault(p=>p.PaperTypeID==photo.PaperTypeID);
                    photoNew.photoName = photo.PhotoName;
                    photoNew.picPath = photo.PicPath;
                    photoNew.pixHeight = photo.PixHeight;
                    photoNew.pixWidth = photo.PixWidth;
                    //photoNew.printerId = 0;
                    photoNew.Remark = photo.Remark;
                    photoNew.status = (int)PhotoStatus.NoDistributePrinter;
                    photoNew.status2 = (int)PhotoStatus2.normal;
                    photoNew.TotalNum = photo.TotalNum;
                    photoNew.userId = 0;
                    photoNew.printerId = 0;
                    photoNew.MakeUpNum = 0;
                    photoNew.Width = photo.Width;
                    orderNew.photoInfo.Add(photoNew);
                }

                CreateOrderFlowList(db, orderNew, inStartTech, inOrder,inUserId);
                try
                {
                    db.SaveChanges();
                    #region//更新orderinfo、photoinfo、tb_orderbag中的对应的CODE
                    foreach (var oi in orderBagNew.orderInfo)
                    {
                        oi.OrderProductCode = "2" + oi.OrderProductId.ToString().PadLeft(9, '0');
                       
                        foreach (var pi in oi.photoInfo)
                        {
                            pi.PhotoBarCode = "3" + pi.photoId.ToString().PadLeft(9, '0');
                        }
                    }
                    orderBagNew.BagCode = "1" + orderBagNew.OrderBagId.ToString().PadLeft(9, '0');
                    db.SaveChanges();
                    #endregion
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

            }
            return "OK";
        }
        private void CreateOrderFlowList(newSampleYPDBEntities db, orderInfo oi,int inStartTechid,OrderInfoInfo inOldOrder,int inuserid)
        {
            if (oi != null && oi.Tb_Product != null)
            {
                if (!oi.Tb_Product.Tb_FlowTemplate.IsLoaded)
                {
                    oi.Tb_Product.Tb_FlowTemplate.Load();
                }

                DateTime assignTime = DateTime.Now;
                OrderFlowListStatus status = OrderFlowListStatus.Complete;
               
                int workOrderStart=0;
                foreach (var ft in db.Tb_FlowTemplate.Where(t => t.Tb_Product.ProuctId == oi.Tb_Product.ProuctId).ToList().OrderBy(f => f.WorkOrder))
                {
                    if (!ft.tb_TechReference.IsLoaded)
                    {
                        ft.tb_TechReference.Load();
                    }
                   
                    oi.WorkOrderFlowList.Add(new WorkOrderFlowList()
                    {
                        AssignTime = assignTime,
                        CompletedTime = assignTime.AddHours(ft.tb_Tech.NeedTime),
                        IsEdit = false,
                        PreCompleteTime = assignTime.AddHours(ft.tb_Tech.NeedTime),
                        ReceiveTime = assignTime,
                        Status = (int)status,
                        tb_Tech = ft.tb_Tech,
                        UserId = 0,
                        WorkOrder = ft.WorkOrder
                    });
                    assignTime = assignTime.AddHours(ft.tb_Tech.NeedTime);
                    if (ft.tb_Tech.TechId == inStartTechid)
                    {
                        workOrderStart = ft.WorkOrder;

                    }
                }
                //if (workOrderStart == 0)
                //{
                //    workOrderStart = oi.WorkOrderFlowList.OrderBy(p=>p.WorkOrder).FirstOrDefault().WorkOrder;
                //}
                foreach (WorkOrderFlowList wofl in oi.WorkOrderFlowList)
                {
                    if (wofl.WorkOrder > workOrderStart)
                    {
                        wofl.Status = (int)OrderFlowListStatus.Wait;
                    }
                    else if (wofl.WorkOrder == workOrderStart)
                    {
                        wofl.Status = (int)OrderFlowListStatus.Useable;

                    }
                    else
                    {
                        wofl.Status = (int)OrderFlowListStatus.Complete;
                        wofl.UserId = inuserid;
                    }
                }

            }
        }

    }
}
